export const displayPrice = function (v : number) {
  return '￥' + (v / 100).toFixed(2)
}

export const displayPriceOrign = function (v : number) {
  return '￥' + v
}

export function loadingShow(hint : string = "加载中") {
  uni.showLoading({
    title: hint
  })
}
export function loadingHide(isSuccess : boolean = false, hint : string = "加载失败") {
  uni.hideLoading()
  if (!isSuccess) {
    showToast(hint)
  }
}



export function base64ToString(content) {
  console.log("解析TOKEN")
  console.log(content)
  const base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  let result = ''
  let e1, e2, e3, e4
  let d1, d2, d3
  let padding = 64;
  let i = 0
  while (i < content.length) {
    e1 = base64Chars.indexOf(content.charAt(i++))
    e2 = base64Chars.indexOf(content.charAt(i++))
    e3 = i < content.length ? base64Chars.indexOf(content.charAt(i++)) : padding
    e4 = i < content.length ? base64Chars.indexOf(content.charAt(i++)) : padding
    d1 = (e1 << 2) | (e2 >> 4)
    d2 = ((e2 & 0xf) << 4) | (e3 >> 2)
    d3 = ((e3 & 0x3) << 6) | e4
    result += String.fromCharCode(d1)
    if (e3 != padding) {
      result += String.fromCharCode(d2)
    }
    if (e4 != padding) {
      result += String.fromCharCode(d3)
    }
  }

  let trim = result.slice(0, -1);
  console.log("转换后数据---：" + trim)
  return result
}

export function parseToken(token) {
  console.log('解析Token数据')
  console.log(token)
  let tokenArr = token.split('.')
  console.log(tokenArr)
  let json = base64ToString(tokenArr[1])
  console.log('Json数据')
  console.log(json)
  let tokenInfo = JSON.parse(json)
  console.log(tokenInfo)
  return tokenInfo
}
export function isValidToken(token) {
  let {
    exp
  } = token
  let after30Minutes = Math.floor((new Date()).getTime() / 1000) + 1800
  console.log("有效时间：", exp + "----" + after30Minutes)
  return exp > after30Minutes
}

export function showToast(hint : string) {
  uni.showToast({
    title: hint,
    icon: 'error',
    duration: 2000
  });
}